<template>
  <div class="index">
    <el-row class="row title">
      <el-col :span="24">
        <div>平台配置</div>
      </el-col>
    </el-row>

    <el-card v-for="(item, key) of rowlist" :key="key" :header="item.title">
      <el-row>
        <el-col v-for="(arr, i) of item.data" :key="i" :span="item.row">
          <div>
            {{ arr.title }} :
            <el-input
              v-model="arr.value"
              style="width: 100px"
              placeholder=""
              @input="arr.value = setnumber(arr.value)"
            />{{ arr.unit }} &nbsp; &nbsp; &nbsp;
            <el-button
              type="primary"
              @click="downloadFile(key,arr.type,i)"
            >更新</el-button
            >
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import {
  basis,
  basisupdate,
  node,
  nodeupdate,
  recommend,
  recommendupdate,
  team,
  teamupdate
} from '@/api/platform'
export default {
  name: 'Index',
  data() {
    return {
      rowlist: [
        {
          title: '基础配置',
          row: 12,
          data: [
            {
              title: '闪兑汇率设置： ETI/USDT =',
              value: '',
              unit: '',
              type: ''
            },
            { title: '社区节点购买金额', value: '', unit: 'USDT', type: '2' },
            { title: '燃料燃烧比例配置', value: '', unit: '', type: '3' },
            { title: '股东节点购买金额', value: '', unit: 'USDT', type: '4' }
          ]
        },
        {
          title: '团队收益配置',
          row: 8,
          data: [
            { title: 'V1收益率', value: '', unit: '', type: '1' },
            { title: 'V2收益率', value: '', unit: '', type: '2' },
            { title: 'V3收益率', value: '', unit: '', type: '3' },
            { title: 'V4收益率', value: '', unit: '', type: '4' },
            { title: 'V5收益率', value: '', unit: '', type: '5' },
            { title: 'V6收益率', value: '', unit: '', type: '6' }
          ]
        },
        {
          title: '节点收益配置',
          row: 8,
          data: [
            { title: '社区节点', value: '', unit: '', type: '1' },
            { title: '股东节点', value: '', unit: '', type: '2' },
            { title: '超级节点', value: '', unit: '', type: '3' }
          ]
        },
        {
          title: '推荐配置',
          row: 8,
          data: [
            { title: '第一代', value: '', unit: '', type: '1' },
            { title: '第二代', value: '', unit: '', type: '2' }
          ]
        },
        {
          title: '机器人费用配置（占仓位比例）',
          row: 8,
          data: [
            { title: '月费机器人', value: '', unit: '', type: '1' },
            { title: '年费机器人', value: '', unit: '', type: '2' }
          ]
        }
      ]
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    downloadFile(index, type, i) {
      switch (index) {
        case 0:
          basisupdate({
            type,
            value: this.rowlist[index].data[i].value
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success('更新成功')
              basis()
            }
          })
          break
        case 1:
          teamupdate({
            type,
            value: this.rowlist[index].data[i].value
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success('更新成功')
              team()
            }
          })
          break
        case 2:
          nodeupdate({
            type,
            value: this.rowlist[index].data[i].value
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success('更新成功')
              node()
            }
          })
          break
        default:
          recommendupdate({
            type,
            value: this.rowlist[index].data[i].value
          }).then((res) => {
            if (res.code == 0) {
              this.$message.success('更新成功')
              recommend()
            }
          })
      }
    },
    //
    setnumber(params) {
      params = params || 0
      if (params == '') {
        return
      }
      params = params.replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
      params = params.replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
      params = params
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
      params = params.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') // 只能输入两个小数
      if (params.indexOf('.') < 0 && params != '') {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        params = parseFloat(params)
      }
      if (params >= 100) {
        params = 100
      }
      if (params < 0) {
        params = 0
      }
      return params
      // console.log(price)
    },
    getInfo() {
      basis().then((res) => {
        const data = res.data
        for (const i in this.rowlist[0].data) {
          this.rowlist[0].data[i].value = (data[i] && data[i].confValue) || 0
          // this.rowlist[0].data[i].type = (data[i] && data[i].confKey) || 0;
        }
      })
      node().then((res) => {
        const data = res.data
        for (const i in this.rowlist[2].data) {
          this.rowlist[2].data[i].value = (data[i] && data[i].confValue) || 0
          // this.rowlist[2].data[i].type = (data[i] && data[i].confKey) || 0;
        }
      })
      recommend().then((res) => {
        const data = res.data
        for (const i in this.rowlist[3].data) {
          this.rowlist[3].data[i].value = (data[i] && data[i].confValue) || 0
          this.rowlist[3].data[i].type = (data[i] && data[i].confKey) || 0
        }
        for (const i in this.rowlist[4].data) {
          this.rowlist[4].data[i].value = (data[i + 2] && data[i + 2].confValue) || 0
          this.rowlist[4].data[i].type = (data[i + 2] && data[i + 2].confKey) || 0
        }
      })
      team().then((res) => {
        const data = res.data
        for (const i in this.rowlist[1].data) {
          this.rowlist[1].data[i].value = (data[i] && data[i].confValue) || 0
          this.rowlist[1].data[i].type = (data[i] && data[i].confKey) || 0
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.index {
  padding: 10px 20px;
  img {
    width: 50px;
    height: 50px;
    border-radius: 5px;
    margin: 0 5px;
  }
  .users {
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
  }
  .el-row {
    text-align: center;
    // border-bottom: 1px solid #ebeef5;
    &:last-child {
      border: none;
    }
    div {
      padding: 10px 0;
      text-align: left;
      // border: 1px solid #ddd;
    }
  }
  .row {
    /* border: 1px solid black; */
    margin: 15px 0;
  }
  .title {
    width: 100px;
    border-bottom: 1px solid #cccccc;
    text-align: left;
  }
  .lastRow {
    img,
    span {
      vertical-align: middle;
    }
  }
  .el-card {
    margin-bottom: 10px;
  }
}
</style>
